package com.lovo.test.dao.impl;

import com.lovo.test.dao.BaseDAO;
import com.lovo.test.dao.UserDAO;
import com.lovo.test.pojo.UserDO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDAOImpl extends BaseDAO implements UserDAO {
    /**
     * 根据用户名和密码查询 当前登录用户
     * @param userName 第一个参数是用户名
     * @param pwd 第二个参数是 密码
     * @return 返回一个用户对象
     */
    @Override
    public UserDO getByUserNameAndPwd(String userName, String pwd) {
        this.getConnection();
        statement = null;
        rs = null;
        UserDO user =null;
        String sql="select * from t_user where u_name=? and u_pwd=?";
        try {
            statement =  con.prepareStatement(sql);
            statement.setString(1,userName);
            statement.setString(2,pwd);
            rs=statement.executeQuery();
            if (rs.next()){
                user = new UserDO();
                user.setId(rs.getInt("u_id"));
                user.setUserName(rs.getString("u_name"));
                user.setPassWord(rs.getString("u_pwd"));
                user.setMoney(rs.getInt("u_moneys"));
//
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(con,statement,rs);
        }
        return user;
    }

    /**
     * 根据 id 查询用户
     * @param id 传入要查询用户的id
     * @return 返回查询对象
     */
    @Override
    public UserDO getById(int id) {
        this.getConnection();
        statement = null;
        rs = null;
        UserDO user = null;
        String sql = "select * from t_user where u_id=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setInt(1,id);
            rs = statement.executeQuery();
            if(rs.next()){
                user = new UserDO();
                user.setId(rs.getInt("u_id"));
                user.setUserName(rs.getString("u_name"));
                user.setPassWord(rs.getString("u_pwd"));
                user.setMoney(rs.getInt("u_moneys"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con,statement,rs);
        }
        return user;
    }

    /**
     * 根据用户名查询用户
     * @param name 输入用户名
     * @return 返回查询对象
     */
    @Override
    public UserDO getByName(String name) {
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        UserDO user = null;
        String sql = "select * from t_user where u_name=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setString(1,name);
            rs = statement.executeQuery();
            if(rs.next()){
                user = new UserDO();
                user.setId(rs.getInt("u_id"));
                user.setUserName(rs.getString("u_name"));
                user.setPassWord(rs.getString("u_pwd"));
                user.setMoney(rs.getInt("u_moneys"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con,statement,rs);
        }
        return user;
    }


    @Override
    public void update(UserDO userDO) {
        //创建连接
        this.getConnection();
        statement = null;
        String sql = "update t_user set u_moneys=?,u_pwd=? where u_id=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setInt(1,userDO.getMoney());
            statement.setString(2, userDO.getPassWord());
            statement.setInt(3,userDO.getId());
            statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement);
        }

    }

    @Override
    public void turnUpdate(UserDO myUser, UserDO objUser) {
        this.getConnection();
        statement = null;
        String sql = "update t_user set u_moneys=?,u_pwd=? where u_id=?";
        try {
            con.setAutoCommit(false);
            statement = con.prepareStatement(sql);
            statement.setInt(1,myUser.getMoney());
            statement.setString(2, myUser.getPassWord());
            statement.setInt(3,myUser.getId());
            statement.executeUpdate();


            statement = con.prepareStatement(sql);
            statement.setInt(1,objUser.getMoney());
            statement.setString(2, objUser.getPassWord());
            statement.setInt(3,objUser.getId());
            statement.executeUpdate();
            con.commit();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } finally {
            close(con, statement);
        }
    }
}
